package com.example.shaidemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TipsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TipsFragment extends Fragment implements AdapterView.OnItemClickListener{

    private SimpleAdapter listItemAdapter; // 适配器
    ListView listView;
    SimpleAdapter simpleAdapter;
    private static final String TAG = "TipsFragment";

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public TipsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TipsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TipsFragment newInstance(String param1, String param2) {
        TipsFragment fragment = new TipsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view =  inflater.inflate(R.layout.fragment_tips, container, false);

        XBanner banner= view.findViewById(R.id.xbanner);
        List<String> img_list = new ArrayList<>();
        //加载轮播
        img_list.add("https://pic3.zhimg.com/v2-6b08e38f0a65cd1ca271da388952611d_720w.jpg?source=172ae18b");
        img_list.add("https://tse1-mm.cn.bing.net/th/id/R-C.af82cea95bfedf1709979a8fcdcf9a84?rik=FI%2fxAT5Bq4tvmA&riu=http%3a%2f%2f5b0988e595225.cdn.sohucs.com%2fimages%2f20190729%2fe13ca0a33cd849fd88486bc2fd601464.jpeg&ehk=wf2F09mPg0a%2bQkxlXZqLAx1HCNivJ2uL7Uzk4la55yw%3d&risl=&pid=ImgRaw&r=0");
        img_list.add("https://tse1-mm.cn.bing.net/th/id/R-C.dfd4aa56f978485392079b5b1d834c55?rik=b%2fyLpnyJkEXKMg&riu=http%3a%2f%2fwww.xinhuanet.com%2ftalking%2f2018-03%2f30%2f129841126_15223824490261n.jpg&ehk=6v9DsYXOGYTGt2E0EQ0NOGPToFC36VQSWfmoYO%2fLbfA%3d&risl=&pid=ImgRaw&r=0");
        banner.setData(img_list,null);
        // 设置XBanner的页面切换特效
        banner.setPageTransformer(Transformer.Default);
        // 设置XBanner页面切换的时间，即动画时长
        banner.setPageChangeDuration(1000);
        banner.setData(img_list,null);

        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                RequestOptions myOptions = new RequestOptions()
                        .transform(new GlideRoundTransform(getContext(),10));

                Glide
                        .with(getContext())
                        .load(img_list.get(position))
                        .apply(myOptions)
                        .into((ImageView) view);

            }
        });

        listView = (ListView)view.findViewById(R.id.show_tips_list);
        List<Map<String,Object>> list = GetData.tipsList();
        simpleAdapter = new SimpleAdapter(getActivity(), list,
                R.layout.tips_list_view,
                new String[]{"one","two","three","four","five","pic_1","pic_2","pic_4"},
                new int[]{R.id.title,R.id.support,R.id.browse,R.id.collect,R.id.pic_1,
                        R.id.pic_2,R.id.pic_3,R.id.pic_4});
        listView.setAdapter(simpleAdapter);
        //设置事件响应
        listView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(),TipsActivity.class);
        startActivity(intent);
    }
}